package com.qf.shoppingorder.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableRabbit
public class MyRabbitMqConfig {

    @Autowired
    RabbitTemplate rabbitTemplate;

//    @Bean
//    public ConnectionFactory connectionFactory() {
//        CachingConnectionFactory connectionFactory = new CachingConnectionFactory("43.139.170.86", 5672);
//        connectionFactory.setUsername("lx");
//        connectionFactory.setPassword("lx0826");
//        connectionFactory.setVirtualHost("/");
//        return connectionFactory;
//    }

    @PostConstruct// todo 该注解在配置文件加载完成后执行
    public void initRabbitTemplate() {
        //todo 服务端确认回调 服务端收到则

        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            //broker收到就回调
            System.out.println(correlationData);
        });

        rabbitTemplate.setReturnCallback((message, i, s, s1, s2) -> {

        });
    }

    //    todo Message的json转化
//    @Bean
//    public MessageConverter messageConverter() {
//        return new Jackson2JsonMessageConverter();
//    }

    @Bean
    public AmqpAdmin amqpAdmin(ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }

    @Bean
    public Queue orderDelayQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", "release-exchange");
        args.put("x-dead-letter-routing-key", "release-route");
        args.put("x-message-ttl", 36000);
        Queue queue = new Queue("delay.queue", true, false, false, args);
        return queue;
    }

    @Bean
    public Queue orderReleaseQueue() {
        Queue queue = new Queue("release.queue", true, false, false);
        return queue;
    }

    @Bean
    public Queue orderReleaseQueue2() {
        Queue queue = new Queue("release.queue2", true, false, false);
        return queue;
    }

    @Bean
    public Queue androidQueue() {
        Queue queue = new Queue("android.queue", true, false, false);
        return queue;
    }

    @Bean
    public Exchange releaseExchange() {
        TopicExchange topicExchange = new TopicExchange("release-exchange", true, false);
        return topicExchange;
    }

    @Bean
    public Exchange delayExchange() {
        TopicExchange topicExchange = new TopicExchange("delay-exchange", true, false);
        return topicExchange;
    }

    @Bean
    public Exchange androidExchange() {
        TopicExchange topicExchange = new TopicExchange("android-exchange", true, false);
        return topicExchange;
    }

    // TODO: 2024/3/19 交换器、队列和路由键绑定
    @Bean
    public Binding orderDelayBinding() {
        Binding binding = new Binding("delay.queue",
                Binding.DestinationType.QUEUE,
                "delay-exchange",
                "delay-route",
                null);
        return binding;
    }

    @Bean
    public Binding orderReleaseBinding() {
        Binding binding = new Binding("release.queue",
                Binding.DestinationType.QUEUE,
                "release-exchange",
                "release-route",
                null);
        return binding;
    }

    @Bean
    public Binding orderReleaseBinding2() {
        Binding binding = new Binding("release.queue2",
                Binding.DestinationType.QUEUE,
                "release-exchange",
                "release-route",
                null);
        return binding;
    }

    @Bean
    public Binding androidBinding() {
        Binding binding = new Binding("android.queue",
                Binding.DestinationType.QUEUE,
                "android-exchange",
                "android-route",
                null);
        return binding;
    }
}
